from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from model import User

# 1. 链接数据库等准备工作
engine = create_engine('mysql+pymysql://root:python@localhost:3306/19python')
DBSession = sessionmaker(bind=engine)
session = DBSession()

# 2. 创建新User对象
new_user = User(id=1, username='python-teacher', password="131", email="xxxxxxx@qq.com")
# 3. 添加到session
session.add(new_user)

# 批量插入多个数据库
new_user2 = User(username='python-teacher2', password="132", email="xxxxxxx2@qq.com")
new_user3 = User(username='python-teacher3', password="133", email="xxxxxxx3@qq.com")
new_user4 = User(username='python-teacher4', password="132", email="xxxxxxx4@qq.com")
new_user5 = User(username='python-teacher5', password="133", email="xxxxxxx4@qq.com")
session.add_all([new_user2, new_user3, new_user4, new_user5])

# 4. 提交即保存到数据库
session.commit()

# 5. 关闭session
session.close()
